package com.rocketmq.controller;

import com.rocketmq.constant.RocketMqContant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * RocketMqController
 *
 * @author huangjj
 * @date 2022/10/2715:25
 */
@Api(tags="生产者-API")
@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // 发送消息的实例
    @ApiOperation(value = "发送TOPIC_ROCKET_A消息")
    @RequestMapping("/sendMsg")
    public String sendMessage(String msg) {
        rocketMQTemplate.convertAndSend(RocketMqContant.TOPIC_ROCKET_A, msg);
        return "SUCCESS";
    }

//    // 发送事务消息的实例
//    public void sendMessageInTransaction(String msg) throws InterruptedException {
//        String[] tags = {"TagA", "TagB", "TagC", "TagD", "TagE"};
//        for (int i = 0; i < 10; i++) {
//            Message<String> message = MessageBuilder.withPayload(msg).build();
//            String destination = RocketMqContant.TOPIC_ROCKET_A + ":" + tags[i % tags.length];
//
//            TransactionSendResult sendResult =
//                    rocketMQTemplate.sendMessageInTransaction(destination, message, destination);
//
//            System.out.printf("%s%n", sendResult);
//            Thread.sleep(10);
//        }
//    }
}
